// 项目详情

Page({

    data: {
        project: {},
        members: [],
        teachers: [],
        thumbsUp: {
            checked: false,
            url: "../../../images/good.png"
        },
        collection: {
            checked: false,
            url: "../../../images/collection.png"
        },
        userInfo: '',
        sessionID: '',
        loginFlag: ''
    },

    // 载入页面
    onLoad(e) {
        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            sessionID: wx.getStorageSync('sessionID'),
            loginFlag: wx.getStorageSync('loginFlag')
        });

        // 获取后端数据，显示项目详情
        wx.request({
            url: 'https://www.hschuangsai.com/displayProject?projectId=' + e.id,
            method: 'GET',
            success: (res) => {
                console.log('display-info request list', res);
                let data = res.data.data;
                this.requestSetData(data);
            }
        });
    },
    //下载函数
    download(e){
      var that=this;
      let suffix='';
      const fileArr=this.data.project.fileName.split('.');
      suffix=fileArr[fileArr.length-1];
      console.log("the suffix is"+suffix)
      console.log('url',this.data.project.file)
      wx.downloadFile({
          url: this.data.project.file,
          filePath: wx.env.USER_DATA_PATH + '/' + encodeURI(this.data.project.fileName),
          success: function (res) {
            console.log('下载',res)
              if (res.statusCode == 200) {
                  let filePath = res.filePath
                  console.log('open the document')
                  wx.openDocument({
                      fileType: suffix,
                      filePath: filePath
                  })
              }
          }
      })
  },

    requestSetData(data) {
        this.setData({
            project: data
        });


        wx.request({
            url: 'https://www.hschuangsai.com/displayProject/likeOrCollect?projectId=' + this.data.project.id,
            method: 'GET',
            header: {
                'session': this.data.sessionID
            },
            success: (res) => {
                let data = res.data.data;
                if (data === 1) {
                    this.setData({
                        "thumbsUp.checked": true,
                        "thumbsUp.url": "../../../images/good-fill.png"
                    });
                }
                else if (data === 2) {
                    this.setData({
                        "collection.checked": true,
                        "collection.url": "../../../images/collection-fill.png"
                    })
                }
                else if (data === 3) {
                    this.setData({
                        "thumbsUp.checked": true,
                        "thumbsUp.url": "../../../images/good-fill.png",
                        "collection.checked": true,
                        "collection.url": "../../../images/collection-fill.png"
                    });
                }
            }
        });
    },

    myThumbsUpChange() {
        let id = this.data.project.id,
            likes = this.data.project.likes;
        if (!this.data.thumbsUp.checked) {
            wx.request({
                url: 'https://www.hschuangsai.com/like?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "thumbsUp.checked": true,
                        "thumbsUp.url": "../../../images/good-fill.png",
                        "project.likes": likes + 1
                    });
                }
            })
        }
        else {
            wx.request({
                url: 'https://www.hschuangsai.com/like/cancel?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "thumbsUp.checked": false,
                        "thumbsUp.url": "../../../images/good.png",
                        "project.likes": likes - 1
                    });
                }
            })
        }
    },

    // wx.login()封装
    myLogin(flag) {
        console.log("myLogin flag", flag);
        wx.login({
            success: (res) => {
                wx.request({
                    url: 'https://www.hschuangsai.com/user/login/' + res.code,
                    method: 'GET',
                    success: (res) => {
                        let sessionID = res.data.data.sessionId;
                        wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                        wx.setStorageSync('loginFlag', 1); // 缓存loginFlag
                        this.setData({
                            sessionID: sessionID,
                            loginFlag: 1
                        });
                        if(flag == 1) {
                            console.log("点赞");
                            this.thumbsUpChange();
                        }
                        else {
                            console.log(flag);
                            console.log("收藏");
                            this.collectionChange();
                        }
                    }
                })
            }
        })
    },

    // 登录
    logIn(flag) {
        if (!this.data.userInfo) {
            wx.getUserProfile({
                desc: '用于获取用户头像和昵称',
                success: (res) => {
                    let userInfo = res.userInfo;
                    wx.setStorageSync('userInfo', userInfo); // 缓存userInfo
                    this.setData({
                        userInfo: userInfo
                    });
                    // 重新登录
                    this.myLogin(flag);
                }
            })
        }
        else {
            // 重新登录
            this.myLogin(flag);
        }
    },

    // wx.showModal,wx.login封装
    myShowModal(flag) {
        wx.showModal({
            title: '提示',
            content: '登录后即可操作，是否登录？',
            success: (res) => {
                // 确定
                if (res.confirm) {
                    this.logIn(flag);
                }
            }
        })
    },

    // 点赞/收藏
    change(e) {
        let flag = e.currentTarget.dataset.flag;
        console.log("flag", flag);
        if (this.data.loginFlag != 1) {
            this.myShowModal(flag);
        }
        else {
            if (flag == 1) {
                this.thumbsUpChange();
            }
            else {
                this.collectionChange();
            }
        }
    },

    // 点赞/取消点赞
    thumbsUpChange() {
        let id = this.data.project.id,
            likes = this.data.project.likes;

        if (!this.data.thumbsUp.checked) {
            wx.request({
                url: 'https://www.hschuangsai.com/like?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "thumbsUp.checked": true,
                        "thumbsUp.url": "../../../images/good-fill.png",
                        "project.likes": likes + 1
                    });
                }
            })
        }
        else {
            wx.request({
                url: 'https://www.hschuangsai.com/like/cancel?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "thumbsUp.checked": false,
                        "thumbsUp.url": "../../../images/good.png",
                        "project.likes": likes - 1
                    });
                }
            })
        }
    },

    // 收藏
    collectionChange() {
        let id = this.data.project.id,
            collections = this.data.project.collections;

        if (!this.data.collection.checked) {
            wx.request({
                url: 'https://www.hschuangsai.com/collection?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "collection.checked": true,
                        "collection.url": "../../../images/collection-fill.png",
                        "project.collections": collections + 1
                    });
                }
            })
        }
        else {
            wx.request({
                url: 'https://www.hschuangsai.com/collection/cancel?projectId=' + id,
                method: 'GET',
                header: {
                    session: this.data.sessionID
                },
                success: (res) => {
                    this.setData({
                        "collection.checked": false,
                        "collection.url": "../../../images/collection.png",
                        "project.collections": collections - 1
                    });
                }
            })
        }
    },

    // 分享给好友
    onShareAppMessage() {
        return {
            path: 'pages/mine/index/index',
            title: this.data.project.name,
            success: (res) => {
                wx.showToast({
                    title: '操作成功！',
                    icon: 'success'
                })
            },
            fail: (res) => {
                wx.showToast({
                    title: '操作失败！',
                    icon: '../../../images/fail.png'
                })
            }
        }
    },

    // 分享到朋友圈
    onShareTimeline(res) {
        return {
            title: this.data.project.name,
            query: "test=测试&from=timeline",
            success: (res) => {
                wx.showToast({
                    title: '操作成功！',
                    icon: 'success'
                })
            },
            fail: (res) => {
                wx.showToast({
                    title: '操作失败！',
                    icon: '../../../images/fail.png'
                })
            }
        }
    }
})